import { Button, Tooltip, InputNumber, Input, message } from 'antd';
import { getColorTextLoadMap, onMouseOutBtn, removeDateHmsStr, renderTableText, tableSorter, tooltipLength, priceFormat } from '@/utils';
import { PlusCircleOutlined, DeleteOutlined, AppstoreAddOutlined } from '@ant-design/icons';
import { getLoadMapList, getSelectDataMapList, getTenantID } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
import { findObjectById } from '@/utils/findObjectById';

/** 材料表格 */
const openServiceColumns = (addMaterialModalStatus, editTable, openMaterialModalStatus, delSelectRow, onEditTable, pageType, isHaveContract) => {
  const isDetail: boolean = pageType == 'detail'; // 是否是查看

  let tableConfig: Record<string, any>[] = [
    {
      title: '服务内容',
      dataIndex: 'fuwuName',
      key: 'fuwuName',
      fixed: 'left',
      width: 160,
      sorter: (a: any, b: any) => tableSorter(a, b, 'fuwuName'),
      render: (text: string, record: any, index: number) => {
        return (
          <Input
            value={text}
            onChange={(e) => {
              editTable({ type: 'fuwuName', value: e.target.value, index, addType: 2 });
            }}
            disabled={isDetail}
          />
        );
      },
    },
    {
      title: '服务事项',
      dataIndex: 'fuwuContent',
      key: 'fuwuContent',
      visible: true,
      width: 260,
      sorter: (a: any, b: any) => tableSorter(a, b, 'fuwuContent'),
      render: (text: string, record: any, index: number) => {
        return (
          <Input
            value={text}
            onChange={(e) => {
              editTable({ type: 'fuwuContent', value: e.target.value, index, addType: 2 });
            }}
            disabled={isDetail}
          />
        );
      },
    },
    {
      title: '数量或时间计量',
      dataIndex: 'fuwushijian',
      key: 'fuwushijian',
      visible: true,
      width: 200,
      sorter: (a: any, b: any) => tableSorter(a, b, 'fuwushijian'),
      render: (text: string, record: any, index: number) => {
        return (
          <InputNumber
            value={text}
            onChange={(e: any) => {
              editTable({ type: 'fuwushijian', value: e, index, addType: 2 });
            }}
            disabled={isDetail}
            precision={2}
          />
        );
      },
    },
    {
      title: '服务人员及联系方式',
      dataIndex: 'userOrPhone',
      key: 'userOrPhone',
      width: 200,
      sorter: (a: any, b: any) => tableSorter(a, b, 'userOrPhone'),
      render: (value: number | any, record, index) => {
        return (
          <Input
            value={value}
            onChange={(e) => {
              editTable({ type: 'userOrPhone', value: e.target.value, index, addType: 2 });
              onEditTable({ type: 'userOrPhone', value: e.target.value, index, record });
            }}
            disabled={isDetail}
          />
        );
      },
    },
  ];
  if (pageType != 'detail') {
    tableConfig?.push({
      title: (
        <div style={{ width: '100%', display: 'flex', justifyContent: 'center', alignItems: 'center' }}>
          <Button
            type="link"
            size="small"
            icon={<AppstoreAddOutlined />}
            className="manageBtntTxtStyle"
            onMouseOut={onMouseOutBtn}
            onClick={openMaterialModalStatus}
          >
            选择
          </Button>
          {!isHaveContract ? (
            <Button
              type="link"
              size="small"
              icon={<PlusCircleOutlined />}
              className="manageBtntTxtStyle"
              onClick={() => addMaterialModalStatus(2)}
              onMouseOut={onMouseOutBtn}
            >
              添加行
            </Button>
          ) : null}
        </div>
      ),
      dataIndex: 'otherBtn',
      key: 'otherBtn',
      visible: true,
      width: 120,
      fixed: 'right',
      render: (_text: any, record: any) => {
        return (
          <div className="counterpoartDelBtnStyle">
            <Button
              type="link"
              size="small"
              icon={<DeleteOutlined />}
              className={'manageBtntTxtStyle'}
              onMouseOut={onMouseOutBtn}
              onClick={() => delSelectRow(record)}
            >
              删除
            </Button>
          </div>
        );
      },
    });
  }
  return tableConfig;
};

/** 表格配置 */
const serviceTableConfig = (assetTypeList) => {
  return [
    {
      title: '物料名称',
      dataIndex: 'assetName',
      key: 'assetName',
      fixed: 'left',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetName'),
      render: (text: string) => renderTableText(text, true),
    },
    // {
    //   title: '物料编码',
    //   dataIndex: 'assetCode',
    //   key: 'assetCode',
    //   fixed: 'left',
    //   width: 100,
    //   sorter: (a: any, b: any) => tableSorter(a, b, 'assetCode'),
    //   render: (text: string) => renderTableText(text, true),
    // },
    {
      title: '规格型号',
      dataIndex: 'sourceTypeName',
      key: 'sourceTypeName',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'sourceTypeName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '量化单位',
      dataIndex: 'unitOfMeasurement',
      key: 'unitOfMeasurement',
      visible: true,
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'unitOfMeasurement'),
      render: (text: string) => {
        return <span>{text ? text : '--'}</span>;
      },
    },
    {
      title: '数量',
      dataIndex: 'assectNum',
      key: 'assectNum',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assectNum'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '单价',
      dataIndex: 'assetValue',
      key: 'assetValue',
      visible: true,
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetValue'),
      render: (text: number | any) => {
        return <span>{text ? priceFormat(text) : `0.00`}</span>;
      },
    },
    {
      title: '总价',
      dataIndex: 'assetTotal',
      key: 'assetTotal',
      visible: true,
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetTotal'),
      render: (text: number | any) => {
        return <span>{text ? priceFormat(text) : `0.00`}</span>;
      },
    },
    {
      title: '品牌',
      dataIndex: 'brandUnit',
      key: 'brandUnit',
      visible: true,
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'brandUnit'),
      render: (text: string) => {
        return <span>{text ? text : '--'}</span>;
      },
    },
    {
      title: '来源',
      dataIndex: 'brandSource',
      key: 'brandSource',
      visible: true,
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'brandSource'),
      render: (text: string) => {
        return <span>{text ? text : '--'}</span>;
      },
    },
  ];
};

export { serviceTableConfig, openServiceColumns };
